// 获取一串随机字符，默认32位
export const clientToken = (num = 32) => {
  // 用于取值的字符串
  const str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz1234567890'
  // 用于临时存放的数组
  const arr = []
  // 遍历到传入的 num 值的次数
  for (let index = 0; index < num; index++) {
    // 每次遍历生成一个值Push到数组
    arr.push(str.charAt(Math.floor(Math.random() * str.length)))
  }
  // 吧数组转换为字符串输出
  return arr.join('')
}

/*
  日期计算
*/

const today = new Date()
const year = today.getUTCFullYear()
const week = today.getDay()
const month = today.getMonth() + 1 < 10 ? '0' + (today.getMonth() + 1) : today.getMonth() + 1
const day = today.getDate() < 10 ? '0' + today.getDate() : today.getDate()

// 获取月初时间
export const getStartDay = () => `${year}-${month}-01 00:00:00`
// 获取月末时间
export const getEndDay = () => `${year}-${month}-` + (new Date(year, month, 0).getDate() < 10 ? '0' + new Date(year, month, 0).getDate() : new Date(year, month, 0).getDate()) + ' 23:59:59'
// 获取年初时间
export const yearStar = () => `${year}-01-01 00:00:00`
// 获取年末时间
export const yearEnd = () => `${year}-12-31 23:59:59`
// 获取今天结束
export const getToday = () => `${year}-${month}-${day} 23:59:59`
// 获取这周开始结束(如果没满就周一到今天,如果是周一二三就获取整周)
export const getWeekToday = () => {
  const oneDay = 24 * 60 * 60 * 1000
  if (week === 0) {
    const nyaTime = new Date(Date.now() - 6 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else if (week === 6) {
    const nyaTime = new Date(Date.now() - 5 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else if (week === 5) {
    const nyaTime = new Date(Date.now() - 4 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二', '星期三', '星期四', '星期五'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else if (week === 4) {
    const nyaTime = new Date(Date.now() - 3 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二', '星期三', '星期四'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else if (week === 3) {
    const nyaTime = new Date(Date.now() - 2 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二', '星期三'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else if (week === 2) {
    const nyaTime = new Date(Date.now() - 2 * oneDay)
    const nyaDay = nyaTime.getDate() < 10 ? '0' + nyaTime.getDate() : nyaTime.getDate()
    const nyaMonth = nyaTime.getMonth() + 1 < 10 ? '0' + (nyaTime.getMonth() + 1) : nyaTime.getMonth() + 1
    return {
      data: ['星期一', '星期二'],
      start: year + '-' + nyaMonth + '-' + nyaDay,
      end: year + '-' + month + '-' + day
    }
  } else {
    return {
      data: ['星期一'],
      start: getToday().split(' ')[0],
      end: getToday().split(' ')[0]
    }
  }
}
// 用于API请求的时间
export const getQueryTime = () => ({
  start: getStartDay(),
  end: getToday()
})
